namespace chatroom{
    class friend_apply_Table{
        public:
            chat_session_Table(std::shared_ptr<odb::core::database>& db):_db(db){}
            // 新增好友申请事件
            bool insert(friend_apply& friend)
            {
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    _db->persist(friend);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("申请好友事件失败{},{}",friend.get_user_id(),e.what());
                    return false;
                }
                return true;
            }
            // 删除好友申请事件
            bool remove(const std::string& uid,const std::string& pid)
            {
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    _db->erase_query<friend_apply>(query::user_id == uid,query::peer_id==pid);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("{}-{}删除好友事件失败,{}",uid,pid,e.what());
                    return false;
                }
                return true;
            }
            //判断是否已经存在申请事件了
            bool exist(const std::string& uid,const std::string& pid)
            {
                bool flag = false;
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    auto ret = _db->query<friend_apply>(query::user_id == uid,query::peer_id==pid);
                    flag = !ret.empty();
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("{}-{}删除好友事件失败,{}",uid,pid,e.what());
                    return false;
                }
                return flag;
            }
            //获取当前指定用户的 所有好友申请者ID
            std::vector<std::string> applyUser(const std::string& uid)
            {
                std::vector<std::string> res;
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::friend_apply> query;
                    typedef odb::result<chatroom::friend_apply> result;
                    result r(_db->query<chatroom::friend_apply>(query::user_id==uid));
                    for(auto i(r.begin()); i != r.end(); ++i)
                    {
                        res.push_back(i->get_peer_id());
                    }
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("获取所有好友申请者ID失败{},{}",uid,e.what());
                    return res;
                }
                return res;
            }
        private:
            std::shared_ptr<odb::core::database> _db
    };
}